﻿using JT809.Protocol.Configs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JTT809_2011_Service.Extensions
{
    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// 自定义配置
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        /// <param name="steup"></param>
        public static void AddIPEndPointConfig(this IServiceCollection services, IConfiguration configuration, Action<IPEndPointOptions> steup)
        {
            services.Configure(steup);
            services.Configure<IPEndPointOptions>(options =>
            {
                configuration.GetSection("IPEndPointOptions").Bind(options);
            });
            services.AddSingleton<IPEndPointOptions>(sp =>
            {
                var options = sp.GetService<IOptions<IPEndPointOptions>>()?.Value ?? throw new ArgumentNullException(nameof(IPEndPointOptions));
                return new IPEndPointOptions(options.Ip,options.Port);       
            });
        }
        /// <summary>
        /// 配置1
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        public static void AddIPEndPointConfig(this IServiceCollection services, IConfiguration configuration)
        {
            services.Configure<IPEndPointOptions>(options =>
            {
                configuration.GetSection("IPEndPointOptions").Bind(options);
            });
            services.AddSingleton<IPEndPointOptions>(sp =>
            {
                var options = sp.GetService<IOptions<IPEndPointOptions>>()?.Value ?? throw new ArgumentNullException(nameof(IPEndPointOptions));
                return new IPEndPointOptions(options.Ip, options.Port);
            });
        }


        /// <summary>
        /// 自定义配置
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        /// <param name="steup"></param>
        public static void AddJT809_0x1001Options(this IServiceCollection services, IConfiguration configuration, Action<JT809_0x1001Options> steup)
        {
            services.Configure(steup);
            services.Configure< JT809_0x1001Options> (options =>
            {
                configuration.GetSection("JT809_0x1001Options").Bind(options);
            });
            services.AddSingleton<JT809_0x1001Options>(sp =>
            {
                var options = sp.GetService<IOptions<JT809_0x1001Options>>()?.Value ?? throw new ArgumentNullException(nameof(JT809_0x1001Options));
                return new JT809_0x1001Options 
                {
                    DownLinkIP=options.DownLinkIP,
                    DownLinkPort=options.DownLinkPort,
                    MsgGNSSCENTERID=options.MsgGNSSCENTERID,
                    Password=options.Password,
                    UserId=options.UserId
                };
            });
        }
        /// <summary>
        /// 配置1
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        public static void AddIJT809_0x1001Options(this IServiceCollection services, IConfiguration configuration)
        {
            services.Configure<JT809_0x1001Options>(options =>
            {
                configuration.GetSection("JT809_0x1001Options").Bind(options);
            });
            services.AddSingleton<JT809_0x1001Options>(sp =>
            {
                var options = sp.GetService<IOptions<JT809_0x1001Options>>()?.Value ?? throw new ArgumentNullException(nameof(JT809_0x1001Options));
                return new JT809_0x1001Options
                {
                    DownLinkIP = options.DownLinkIP,
                    DownLinkPort = options.DownLinkPort,
                    MsgGNSSCENTERID = options.MsgGNSSCENTERID,
                    Password = options.Password,
                    UserId = options.UserId
                };
            });
        }

        public static void AddJT809HeaderOptions(this IServiceCollection services, IConfiguration configuration, Action<JT809HeaderOptions> steup)
        {
            services.Configure(steup);
            services.Configure<JT809_0x1001Options>(options =>
            {
                configuration.GetSection("JT809HeaderOptions").Bind(options);
            });
            services.AddSingleton<JT809HeaderOptions>(sp =>
            {
                var options = sp.GetService<IOptions<JT809HeaderOptions>>()?.Value ?? throw new ArgumentNullException(nameof(JT809HeaderOptions));
                return new JT809HeaderOptions
                {
                   EncryptFlag=options.EncryptFlag,
                   EncryptKey=options.EncryptKey,
                   MsgGNSSCENTERID=options.MsgGNSSCENTERID,
                   Version=options.Version
                };
            });
        }

        public static void AddJT809HeaderOptions(this IServiceCollection services, IConfiguration configuration)
        {
            services.Configure<JT809_0x1001Options>(options =>
            {
                configuration.GetSection("JT809HeaderOptions").Bind(options);
            });
            services.AddSingleton<JT809HeaderOptions>(sp =>
            {
                var options = sp.GetService<IOptions<JT809HeaderOptions>>()?.Value ?? throw new ArgumentNullException(nameof(JT809HeaderOptions));
                return new JT809HeaderOptions
                {
                    EncryptFlag = options.EncryptFlag,
                    EncryptKey = options.EncryptKey,
                    MsgGNSSCENTERID = options.MsgGNSSCENTERID,
                    Version = options.Version
                };
            });
        }

    }
}
